package servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import com.google.gson.Gson;
import dao.StudentDAO;
import bean.Student;

@WebServlet(name = "SearchServlet")
public class SearchServlet extends HttpServlet {
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String content = request.getParameter("content");
        String type = request.getParameter("type");
        System.out.println("内容为："+content+" 类型为："+type);
        ArrayList<Student> students = new StudentDAO().getStudent(type,content);


        Gson gson = new Gson();
        String json = gson.toJson(students);//解析数据生成Json
        for (Student s : students){
            System.out.println("查询结果："+s.getName()+s.getStudentID());
        }

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");

        System.out.println("JSON："+json);
        PrintWriter out = response.getWriter();
        out.print(json);
        out.flush();
        out.close();
    }
}
